const { globSync } = require("glob");
const { readFileSync, writeFileSync } = require("node:fs");

const DIRS = ['pages', 'components',]
const FILE_TYPES = ['vue', 'js']

const clearI18n = () => {
  const map = JSON.parse(readFileSync('locales/zh.json', 'utf8'))
  const newMap = {}
  const keys = Object.keys(map)
  const fileList = []
  for (const DIR of DIRS) {
    const files = globSync(`${DIR}/**/*.{${FILE_TYPES.join(",")}}`)
    fileList.push(...files)
  }
  fileList.map(el => {
    return readFileSync(el, 'utf8')
  }).forEach(content => {
    keys.forEach(key => {
      if (content.includes(key)) {
        newMap[key] = map[key]
      }
    })
  })
  writeFileSync('locales/zh.json', JSON.stringify(newMap, null, 2))
}

clearI18n()